package com.zengqingfa.springboot.es.controller;

import com.zengqingfa.springboot.es.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @fileName: ContentController
 * @author:
 * @date: 2020-7-4 11:45
 * @description:
 */
@RestController
@RequestMapping("/content")
public class ContentController {

    public static final Logger LOGGER = LoggerFactory.getLogger(ContentController.class);

    @Autowired
    private ContentService contentService;

    @GetMapping("/parse/{keywords}")
    public Boolean parse(@PathVariable("keywords") String keywords) {
        try {
            return contentService.parseContent(keywords);
        } catch (Exception e) {
            LOGGER.error("e:{}", e.getMessage());
        }
        return false;
    }


    @GetMapping("/search/{keywords}")
    public List<Map<String, Object>> searchPage(@PathVariable("keywords") String keywords,
                                                @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        try {
            return contentService.searchPage(keywords, pageNo, pageSize);
        } catch (Exception e) {
            LOGGER.error("e:{}", e.getMessage());
        }
        return new ArrayList<>();
    }


    @GetMapping("/search/hightlight/{keywords}")
    public List<Map<String, Object>> searchPageHightLight(@PathVariable("keywords") String keywords,
                                                @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }
        try {
            return contentService.searchPageHightLight(keywords, pageNo, pageSize);
        } catch (Exception e) {
            LOGGER.error("e:{}", e.getMessage());
        }
        return new ArrayList<>();
    }

}
